/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codebar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author yole
 */
public class CodeList {
 
    private ArrayList<CodeNode> codeNodes;
    
    public CodeList() {
        codeNodes = new ArrayList<CodeNode>();
    }
    
    public void clear() {
        codeNodes.clear();
    }
    
    public void sortProc() {
       
        for ( int i = 0; i < codeNodes.size(); i++) {
            codeNodes.get(i).sort();
        }
    }
    
    public void add(String year, String nordem) throws DuplicatedDataException {
        
            if (indexOf(year) == -1) {
                codeNodes.add(new CodeNode(year));
                Collections.sort(codeNodes);
            }
          
            codeNodes.get(indexOf(year)).add(nordem);
            
            sortProc();
       
    }
    
    public int total() {
        int s = 0;
        for (int i = 0; i < codeNodes.size(); i++) {
            s += codeNodes.get(i).size();
        }
        
        return s;
    }
    
    public int indexOf(String year) {
        int i = 0;
        while (i < codeNodes.size()) {
            if (getYear(i).compareTo(year) == 0) return i;
            i++;
        }
        
        return -1;
    }
    
    public void remove(String year, String nordem) throws NotFoundException {
        if (indexOf(year) == -1) throw new NotFoundException();
        else {
            int i = indexOf(year);
            codeNodes.get(i).remove(nordem);
            if (codeNodes.get(i).size() == 0)
                codeNodes.remove(i);
        }
    }
    
    public int size() {
        return codeNodes.size();
    }
    
    public String getYear(int i) {
        return codeNodes.get(i).getYear();
    }
    
    public CodeNode get(int i) {
        return codeNodes.get(i);
    }
    
    public CodeNode get(String year) {
        CodeNode N = null;
        
        for ( int i = 0; i < codeNodes.size(); i++ ) {
            if (codeNodes.get(i).getYear().compareTo(year) == 0)
                N = codeNodes.get(i);
        }
        
        
        return N;
    }
    
}
